# -*- coding:utf-8 -*-
import os
import codecs
import configparser

# global proDir
from utils.io.fileutils import getFilePath


# proDir = os.path.split(os.path.realpath(__file__))[0]
# configPath = os.path.join(proDir, "config.ini")

# print(configPath)


class configNoCase(configparser.RawConfigParser):
    '''
    大小写不敏感的 configpaser，原来全部转换为 小写了
    '''

    def __init__(self, defaults=None):
        configparser.ConfigParser.__init__(self, defaults=None)

    def optionxform(self, optionstr):
        return optionstr


class ReadConfig:
    def __init__(self):
        # fd = open(configPath)
        # data = fd.read()
        #
        # #  remove BOM
        # if data[:3] == codecs.BOM_UTF8:
        #     data = data[3:]
        #     file = codecs.open(configPath, "w")
        #     file.write(data)
        #     file.close()
        # fd.close()
        self.cf = configNoCase()
        self.cf.read(getFilePath("config.ini"), encoding="utf-8")

    def get_email(self, name):
        value = self.cf.get("EMAIL", name)
        return value

    def get_url(self, name):
        value = self.cf.get("URL", name)
        return value

    # dbname连接不同的数据库
    def get_db(self, dbname, name):
        value = self.cf.get(dbname, name)
        return value

    def get_http(self, httpname, name):
        value = self.cf.get(httpname, name)
        return value


class ConfigFileReader(ReadConfig):
    def __init__(self, section):
        self.section = section
        super().__init__()

    def getValue(self, keyname):
        return self.get_http(self.section, keyname)


if __name__ == "__main__":
    localReadConfig = ReadConfig()
    print(localReadConfig.get_http("HTTPfljrzzbiao", "baseurl"))
    print(str(localReadConfig.cf.defaults()))
